package com.rssfeed.adaper;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.rssfeed.model.RSSFeed;
import com.rssfeed.voa.R;


public class RssAdapter extends ArrayAdapter<RSSFeed>{
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	private List<RSSFeed> rssFeedLst;
	private Context mContext;
	DisplayImageOptions options;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

    public RssAdapter(Context context, int textViewResourceId, List<RSSFeed> rssFeedLst) {
        super(context, textViewResourceId, rssFeedLst);
        this.rssFeedLst = rssFeedLst;
        mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

    	options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_stub)
		.showImageForEmptyUri(R.drawable.ic_empty)
		.showImageOnFail(R.drawable.ic_error)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.displayer(new RoundedBitmapDisplayer(20))
		.build();

        View view = convertView;
        RssHolder rssHolder = null;
        if (convertView == null) {
            view = View.inflate(mContext, R.layout.rssfeedadapter_layout, null);
            rssHolder = new RssHolder();
            rssHolder.rssTitleView = (TextView) view.findViewById(R.id.feed_text);
            rssHolder.image = (ImageView) view.findViewById(R.id.feed_image);
            view.setTag(rssHolder);
        } else {
            rssHolder = (RssHolder) view.getTag();
        }
        RSSFeed rssFeed = rssFeedLst.get(position);
        rssHolder.rssTitleView.setText(rssFeed.getTitle());
        String imgLink = rssFeed.getImgLink();
        imageLoader.displayImage(imgLink, rssHolder.image, options,animateFirstListener);

        
        return view;
    }
    
    static class RssHolder {
        public TextView rssTitleView;
        public ImageView image;
    }
    
    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
}
